package com.yycx.module.bus.provider.handler.haiying.tasktodo.settle.apply;

import com.yycx.bpm.provider.listener.FlowPageInterceptor;
import com.yycx.bpm.provider.model.TaskResponse;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.provider.service.*;
import com.yycx.module.bus.provider.service.hy.BusHyorderInoviceDetailsService;
import com.yycx.module.system.provider.service.SysCompanyService;
import org.flowable.common.engine.api.query.Query;
import org.flowable.engine.RuntimeService;
import org.flowable.task.api.TaskInfoQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 结算费用申请审批任务列表
 *
 * @author zyf
 */
@Component
public class HySettleApplyTab1ListHandler implements FlowPageInterceptor<TaskResponse> {

    @Autowired
    private BusPaymentService paymentService;


    @Autowired
    private BusDeptService deptService;


    @Autowired
    private BusSalemanService salemanService;

    @Autowired
    private SysCompanyService companyService;

    @Autowired
    private BusSalemanBankService salemanBankService;


    @Autowired
    protected RuntimeService runtimeService;

    @Autowired
    private BusHyorderInoviceDetailsService orderInoviceDetailsService;


    @Override
    public void prepare(Query query, EntityMap params) {
        TaskInfoQuery taskQuery = (TaskInfoQuery) query;
        Long companyId = OpenHelper.getCompanyId();
        String paymentNo = params.get("paymentNo");
        String deptId = params.get("deptId");
        String supplierId = params.get("supplierId");
        String customId = params.get("customId");
        String amountType = params.get("amountType");
        String taskName = params.get("taskName");
        String linkMan = params.get("linkMan");
        BusPayment payment = paymentService.getByProperty("paymentNo", paymentNo);
        if (FlymeUtils.isNotEmpty(payment)) {
            taskQuery.processInstanceBusinessKey(payment.getPaymentId().toString());
        } else {
            if (FlymeUtils.isNotEmpty(paymentNo)) {
                taskQuery.processVariableValueLike("paymentNo", '%' + paymentNo);
            }
        }
        if (FlymeUtils.isNotEmpty(amountType)) {
            taskQuery.processVariableValueEquals("amountType", amountType);
        }
        if (FlymeUtils.isNotEmpty(deptId)) {
            taskQuery.processVariableValueEquals("deptId", Long.parseLong(deptId));
        }
        if (FlymeUtils.isNotEmpty(linkMan)) {
            taskQuery.processVariableValueLike("linkMan", linkMan);
        }
        if (FlymeUtils.isNotEmpty(supplierId)) {
            taskQuery.processVariableValueEquals("supplierId", Long.parseLong(supplierId));
        }
        if (FlymeUtils.isNotEmpty(customId)) {
            taskQuery.processVariableValueEquals("customId", Long.parseLong(customId));
        }
        if (FlymeUtils.isNotEmpty(taskName)) {
            taskQuery.taskName(taskName);
        }
        //排除发起人节点
        taskQuery.taskDefinitionKeyLike("Activity_%");
        //只查看当前账套数据
        taskQuery.processVariableValueEquals("companyId", companyId);
        //关联租户ID
        taskQuery.taskTenantId("flp").orderByTaskCreateTime().desc();
        taskQuery.processDefinitionKey("process_settle_pay_flp");
    }

    @Override
    public List<EntityMap> complete(Query query, List<TaskResponse> data, EntityMap params) {
        List<EntityMap> list = new ArrayList<>();
        if (FlymeUtils.isNotEmpty(data)) {
            for (TaskResponse taskResponse : data) {
                EntityMap map = new EntityMap();
                map.put("processInstanceId", taskResponse.getProcessInstanceId());
                String paymentId = taskResponse.getBusinessKey();
                String taskName = taskResponse.getName();
                String taskDefinitionKey = taskResponse.getTaskDefinitionKey();
                if (FlymeUtils.isNotEmpty(paymentId)) {

                    BusPayment payment = paymentService.getById(paymentId);
                    if (FlymeUtils.isNotEmpty(payment)) {
                        //单据类型
                        String amountType = payment.getAmountType();
                        map.put("settleType", payment.getSettleType());
                        map.put("paymentNo", payment.getPaymentNo());
                        map.put("paymentId", payment.getPaymentId());
                        map.put("drugType", payment.getDrugType());
                        map.put("nickName", payment.getNickName());
                        map.put("payeeName", payment.getPayeeName());
                        map.put("saleManCode", payment.getSaleManCode());
                        map.put("deptName", payment.getDeptName());
                        map.put("remark", payment.getRemark());
                        map.put("amountType", amountType);
                        map.put("amount", payment.getAmount());
                        map.put("createTime", payment.getCreateTime());
                        map.put("amountDate", DateUtils.formatDate(payment.getAmountDate(), "yyyy-MM-dd"));
                        map.put("paymentState", payment.getPaymentState());
                        map.put("assignee", taskResponse.getAssignee());
                        map.put("taskName", taskName);
                        map.put("taskDefinitionKey", taskDefinitionKey);
                        map.put("taskId", taskResponse.getId());
                        map.put("endTime", taskResponse.getEndTime());
                    }
                    list.add(map);
                }
            }
        }
        return list;
    }
}
